package org.come.control;

import java.io.IOException;
import java.math.BigDecimal;

import org.come.Frame.ZhuFrame;
import org.come.Jpanel.FrameMessageChangeJpanel;
import org.come.action.NpcMenuAction;
import org.come.bean.NpcCureBean;
import org.come.entity.RoleSummoning;
import org.come.mouslisten.PetAddPointMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.Article;
import org.come.until.GsonUtil;
import org.come.until.UserData;
import org.come.until.UserMessUntil;

import com.tool.image.ImageMixDeal;
import com.tool.pet.PetProperty;

/**
 * 
 * @author 黄建彬
 *
 *查看召唤兽亲密度
 */

public class RoleSumminCloseControl  implements NpcMenuAction{

	@Override
	public void menuControl(String type) {
		if ("治疗".equals(type)) {
			cureRolesumming();
		} else if ("治疗所有".equals(type)) {
			cureAllPet();
		} else {// 查看亲密
			getRolesummingFaithFful();
		}
	}
	/**查看召唤兽的忠诚度*/
	public void getRolesummingFaithFful() {
		if (UserMessUntil.getChosePetMes() != null) {
			FrameMessageChangeJpanel.addtext(6, "您召唤兽的亲密度是"+ UserMessUntil.getChosePetMes().getFriendliness(), null,null);
			// 头像提示框
			ImageMixDeal.userimg.Dialogue("您召唤兽的亲密度是"+ UserMessUntil.getChosePetMes().getFriendliness());
		} else {
			FrameMessageChangeJpanel.addtext(6, "您没有携带召唤兽！！", null, null);
			// 头像提示框
			ImageMixDeal.userimg.Dialogue("您没有携带召唤兽！！");
		}
	}
	/**治疗召唤兽的信息*/
	public void cureRolesumming() {
		if (UserMessUntil.getChosePetMes() != null) {
			try {
				addPetFathiful(UserMessUntil.getChosePetMes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			FrameMessageChangeJpanel.addtext(6, "您没有携带召唤兽！！", null, null);
			// 头像提示框
			ImageMixDeal.userimg.Dialogue("您没有携带召唤兽！！");
		}
	}
	/**增加召唤兽的忠诚度，100满忠诚   ,总血量*/
	public void addPetFathiful(RoleSummoning roleSummoning) throws IOException {
		int[] pets = PetProperty.getPetHMASp(roleSummoning);
		// 召唤兽当前忠诚度
		long petFaiful = roleSummoning.getFaithful();
		// 当前血量
		long petHp = roleSummoning.getBasishp();
		// 总血量
		long sumhp = pets[0];
		// 当前蓝量
		long petMp = roleSummoning.getBasismp();
		// 总蓝量
		int summp = pets[1];
		// 全部加满需要的金钱
		BigDecimal sumMoney = new BigDecimal((((sumhp - petHp)+ (summp - petMp) + (100 - petFaiful))) / 15);
		// 剩余钱数
		// BigDecimal leaveMoney=nowMoney.subtract(sumMoney);
		// 先判断当前的血量和忠诚度是否都满
		if (petMp >= summp && petHp >= sumhp && petFaiful == 100) {
			// 提示已满信息
			// ZhuFrame.getZhuJpanel().addPrompt2("您携带的召唤兽血量和忠诚度都已满！！");
		} else {
			if (sumMoney.longValue()<=0) {
				sumMoney=new BigDecimal(10000);
			}
			if (UserData.uptael(sumMoney.longValue())) {
				// 当前余额够用来将召唤兽的忠诚度进行修复
				// 将当前召唤兽忠诚度设置为满
				roleSummoning.setFaithful(100);
				// 设置血量为满
				roleSummoning.setBasishp(Integer.parseInt(sumhp+""));
				roleSummoning.setBasismp(summp);
				// 设置返回的信息
				NpcCureBean npcCureBean = new NpcCureBean();
				// 设置此时召唤兽的信息
				npcCureBean.setRoleSummoning(roleSummoning);
				// 将选中的召唤兽设置为当前状态的召唤兽
				UserMessUntil.setChosePetMes(roleSummoning);
				// 返回给服务器信息
				String messString = Agreement.npccureAgreement(GsonUtil.getGsonUtil().getgson().toJson(npcCureBean));
				// 向服务器发送信息
				SendMessageUntil.toServer(messString);
				ZhuFrame.getZhuJpanel().addPrompt2("您的召唤兽忠诚度和血量已经回复了。。");
				ZhuFrame.getZhuJpanel().addPrompt2("您扣除了" + sumMoney + "两金币！！");
				// 刷新召唤兽血条
				Article.souxie(UserMessUntil.getChosePetMes());
				// 刷新召唤兽面板
				PetAddPointMouslisten.showPetValue();
			} else {
				// 当前余额不够用来支付召唤兽的忠诚度
				ZhuFrame.getZhuJpanel().addPrompt2("您没有足够的金钱修复您的召唤兽忠诚和血量！！");
			}
		}
	}
	 /**
	  * 一键修复所有的召唤兽技能
	  */
	 public void cureAllPet(){
		if(UserMessUntil.getPetListTable().size()>0){
		 
		 for (int i = 0; i <UserMessUntil.getPetListTable().size(); i++) {
			
			 try {
				addPetFathiful(UserMessUntil.getPetListTable().get(i));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
		}}
		 else{
			 
			//当前余额不够用来支付召唤兽的忠诚度 
			 ZhuFrame.getZhuJpanel().addPrompt2("你还没有召唤兽！！！");
 
		 }
		 
	 }
}
